﻿using System;
using System.Windows.Forms;
using System.Linq;

namespace QuanLyKTX
{
    using DAL;
    using Telerik.WinControls.UI;
    public partial class frm_SortRoom : Telerik.WinControls.UI.RadForm
    {
        #region Private Methods

        private void BindPhong(bool loaiPhong)
        {
            using (var db = new QuanLyKTXDataContext())
            {
                var results = from p in db.tbl_Phongs
                              where p.LoaiPhong == loaiPhong
                              select p;
                if (results.Count() > 0)
                {
                    radDropDownList_SoPhong.ValueMember = "SoPhong";
                    radDropDownList_SoPhong.DisplayMember = "SoPhong";
                    radDropDownList_SoPhong.DataSource = results;

                    BindSVChuaXepPhong(loaiPhong);

                    BindSVHienCoTrongPhong(radDropDownList_SoPhong.SelectedValue.ToString());
                }                
            }
        }

        private void BindSVChuaXepPhong(bool gioiTinh)
        {
            using (var db = new QuanLyKTXDataContext())
            {
                var results = from p in db.tbl_SinhViens                              
                              where !(from q in db.tbl_XepPhongs select q.ID).Contains(p.ID) && p.GioiTinh == gioiTinh
                              select new
                              {
                                  p.ID,
                                  HoTen = String.Format("{0} {1}", p.Ho, p.Ten),
                                  p.MaSinhVien,
                                  p.SoCMND
                              };

                if (results.Count() > 0)
                {                                        

                    radGridView_SVChuaXepPhong.DataSource = results;
                }
                else
                {
                    radGridView_SVChuaXepPhong.DataSource = null;
                }
            }
        }

        private void BindSVHienCoTrongPhong(string soPhong)
        {
            using (var db = new QuanLyKTXDataContext())
            {
                var results = from p in db.tbl_XepPhongs
                              join q in db.tbl_SinhViens on p.ID equals q.ID
                              where p.SoPhong == soPhong 
                              select new
                              {
                                  q.ID,
                                  q.MaSinhVien,
                                  q.SoCMND,
                                  HoTen = String.Format("{0} {1}", q.Ho, q.Ten)
                              };

                if (results.Count() > 0)
                {                   

                    radGridView_SVCoTrongPhong.DataSource = results;
                }
                else
                {
                    radGridView_SVCoTrongPhong.DataSource = null;
                }
            }
        }

        private void InsXepPhong(string soPhong, int id, DateTime ngayVao)
        {
            using (var db = new QuanLyKTXDataContext())
            {
                if (db.tbl_Phongs.Where(p => p.SoPhong == soPhong && p.SoGiuongTrong > 0).Count() > 0)
                {                    

                    tbl_XepPhong tblXepPhong = new tbl_XepPhong
                    {
                        SoPhong = soPhong,
                        ID = id,
                        NgayVao = radDateTimePicker_NgayVao.Value
                    };

                    db.tbl_XepPhongs.InsertOnSubmit(tblXepPhong);

                    // Update Chỗ trống trong phòng
                    var resultPhong = (from p in db.tbl_Phongs
                                       where p.SoPhong == soPhong && p.SoGiuongTrong > Convert.ToByte(0)
                                       select p).Single();
                    int soGiuongTrong = Convert.ToInt32(resultPhong.SoGiuongTrong) - 1;

                    resultPhong.SoGiuongTrong = Convert.ToByte(soGiuongTrong);
                    db.SubmitChanges();
                }
                else
                {
                    MessageBox.Show("Phòng không còn chỗ trống.");
                }
            }
        }

        private void UpdXepPhong(string soPhong, int id)
        {
            using (var db = new QuanLyKTXDataContext())
            {
                var result = (from p in db.tbl_XepPhongs
                              where p.ID == id && p.SoPhong == soPhong && p.NgayRa == null
                              select p).Single();

                result.NgayRa = DateTime.Now;

                // Tăng số chỗ trống trong phòng
                var resultPhong = (from p in db.tbl_Phongs
                                   where p.SoPhong == soPhong
                                   select p).Single();

                int soGiuongTrong = Convert.ToInt32(resultPhong.SoGiuongTrong) + 1;
                resultPhong.SoGiuongTrong = Convert.ToByte(soGiuongTrong);

                db.SubmitChanges();
            }
        }

        private void DelXepPhong(string soPhong, int id)
        {
            using (var db = new QuanLyKTXDataContext())
            {
                var result = (from p in db.tbl_XepPhongs
                              where p.SoPhong == soPhong && p.ID == id
                              select p).Single();
                if (result != null)
                {
                    db.tbl_XepPhongs.DeleteOnSubmit(result);

                    // Tăng số chỗ trống trong phòng
                    var resultPhong = (from p in db.tbl_Phongs
                                       where p.SoPhong == soPhong
                                       select p).Single();

                    int soGiuongTrong = Convert.ToInt32(resultPhong.SoGiuongTrong) + 1;
                    resultPhong.SoGiuongTrong = Convert.ToByte(soGiuongTrong);

                    db.SubmitChanges();
                }
            }
        }

        private string SoGiuongTrong(string soPhong)
        {
            using (var db = new QuanLyKTXDataContext())
            {
                var result = (from p in db.tbl_Phongs
                              where p.SoPhong == soPhong
                              select p.SoGiuongTrong).FirstOrDefault();

                return string.Format("Còn trống {0} giường", result.ToString());
            }
        }

        #endregion

        #region Event Methods

        public frm_SortRoom()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }

        private void radRadioButton_Nam_ToggleStateChanged(object sender, Telerik.WinControls.UI.StateChangedEventArgs args)
        {
            BindPhong(radRadioButton_Nam.IsChecked);
            radLabel_SoChoTrong.Text = SoGiuongTrong(radDropDownList_SoPhong.SelectedValue.ToString());
        }

        private void radRadioButton_Nu_ToggleStateChanged(object sender, Telerik.WinControls.UI.StateChangedEventArgs args)
        {
            BindPhong(radRadioButton_Nam.IsChecked);
            radLabel_SoChoTrong.Text = SoGiuongTrong(radDropDownList_SoPhong.SelectedValue.ToString());
        }

        private void radButton_Them_Click(object sender, EventArgs e)
        {          

            if (radGridView_SVChuaXepPhong.RowCount > 0)
            {
                GridViewSelectedRowsCollection rowsSelected = radGridView_SVChuaXepPhong.SelectedRows;
                foreach (var row in rowsSelected)
                {
                    InsXepPhong(radDropDownList_SoPhong.SelectedValue.ToString(), Convert.ToInt32(row.Cells[0].Value),radDateTimePicker_NgayVao.Value);
                    //MessageBox.Show(row.Cells[0].Value.ToString());                    
                }

                BindSVChuaXepPhong(radRadioButton_Nam.IsChecked);
                BindSVHienCoTrongPhong(radDropDownList_SoPhong.SelectedValue.ToString());
                radLabel_SoChoTrong.Text = SoGiuongTrong(radDropDownList_SoPhong.SelectedValue.ToString());
            }
        }

        private void radButton_Bot_Click(object sender, EventArgs e)
        {

            if (radGridView_SVCoTrongPhong.RowCount > 0)
            {
                GridViewSelectedRowsCollection rowsSelected = radGridView_SVCoTrongPhong.SelectedRows;
                foreach (var row in rowsSelected)
                {
                    DelXepPhong(radDropDownList_SoPhong.SelectedValue.ToString(), Convert.ToInt32(row.Cells[0].Value));
                }
                BindSVChuaXepPhong(radRadioButton_Nam.IsChecked);
                BindSVHienCoTrongPhong(radDropDownList_SoPhong.SelectedValue.ToString());
                radLabel_SoChoTrong.Text = SoGiuongTrong(radDropDownList_SoPhong.SelectedValue.ToString());
            }
        }

        private void radDropDownList_SoPhong_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            BindSVChuaXepPhong(radRadioButton_Nam.IsChecked);

            if (radDropDownList_SoPhong.Items.Count() > 0)
            {
                radLabel_SoChoTrong.Text = SoGiuongTrong(radDropDownList_SoPhong.SelectedValue.ToString());
                
                BindSVHienCoTrongPhong(radDropDownList_SoPhong.SelectedValue.ToString());
            }
        }

        #endregion

        private void frm_SortRoom_Load(object sender, EventArgs e)
        {
           
        }

    }
}
